package com.superdaxue.rxsampledemo;

import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.Toast;

import com.superdaxue.rxsampledemo.configs.AppConfigs;
import com.superdaxue.rxsampledemo.utils.WebViewUtils;

public class WebActivity extends AppCompatActivity implements WebViewUtils.WebViewLoadCallBack{
    private WebViewUtils webViewUtils;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_web_activity);
        toolbar.setTitle(AppConfigs.WEB_TITLE);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        Intent i = getIntent();
        if (i != null){
            Bundle b = i.getExtras();
            String url = b.getString(AppConfigs.BUNDLE_URL);
            webViewUtils = new WebViewUtils(WebActivity.this,savedInstanceState,url);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home){
            onBackPressed();
            return  true;
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    public void onBackPressed() {
        if (webViewUtils.onBackPressed()){
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        webViewUtils.saveState(outState);
    }

    @Override
    protected void onDestroy() {
        webViewUtils.release();
        super.onDestroy();
    }

    @Override
    public void onUtilPageStarted(WebView view, String url, Bitmap favicon) {

    }

    @Override
    public void onUtilPageFinished(WebView view, String url) {

    }

    @Override
    public void onUtilReceivedError(WebView view, int errorCode, String description, String failingUrl) {
         Toast.makeText(WebActivity.this,AppConfigs.DATA_FAILED,Toast.LENGTH_LONG).show();
    }
}
